package com.qingcheng.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.qingcheng.dao.BrandMapper;
import com.qingcheng.entity.PageResult;
import com.qingcheng.pojo.goods.Brand;
import com.qingcheng.service.goods.BrandService;
import org.springframework.beans.factory.annotation.Autowired;
import tk.mybatis.mapper.entity.Example;

import java.util.List;
import java.util.Map;

@Service
public class BrandServiceImpl implements BrandService {
    @Autowired
    private BrandMapper brandMapper;
    @Override
    public List<Brand> findAll() {

        return brandMapper.selectAll();
    }
//    分页
    @Override
    public PageResult<Brand> findPage(int page, int size) {
        PageHelper.startPage(page,size);

        Page<Brand> page1Result=(Page<Brand>) brandMapper.selectAll();

        return new PageResult<>(page1Result.getTotal(),page1Result.getResult());
    }
//条件查询
    @Override
    public List<Brand> findList(Map<String, Object> searchMap) {


        Example example = createExample(searchMap);
        return brandMapper.selectByExample(example);//example,查询条件的封装对象

    }
    private Example createExample(Map<String, Object> searchMap)
    {
        Example example=new Example(Brand.class);

        Example.Criteria criteria = example.createCriteria();
//        家条件
        if (searchMap!=null)
        {
//            模糊查询
            if (searchMap.get("name")!= null && !"".equals(searchMap.get("name")))
            {
                criteria.andLike("name","%"+(String)searchMap.get("name")+"%");
            }
//            精准查询
            if (searchMap.get("letter")!= null && !"".equals(searchMap.get("letter")))
            {
                criteria.andEqualTo("letter",(String)searchMap.get("letter"));
            }

        }
        return example;
    }

    //    条件+分页查询
    @Override
    public PageResult<Brand> findPage(Map<String, Object> searchMap, int page, int size) {
        PageHelper.startPage(page,size);

        Example example = createExample(searchMap);
        Page<Brand> page1Result=(Page<Brand>) brandMapper.selectByExample(example);



        return new PageResult<>(page1Result.getTotal(),page1Result.getResult());


    }

    @Override
    public Brand findById(Integer id) {
//        根据id查询
        Brand brand = brandMapper.selectByPrimaryKey(id);

        return brand;
    }

//    增加手机
    @Override
    public void add(Brand brand) {
        brandMapper.insert(brand);

    }

    @Override
    public void update(Brand brand) {
        brandMapper.updateByPrimaryKeySelective(brand);
    }

    @Override
    public void delete(Integer id) {
        brandMapper.deleteByPrimaryKey(id);
    }

}
